<script>
	import Vue from 'vue'
	import request from "./common/request.js";
	import * as constants from '@/common/config.js';
	import WebSocketClass from '@/common/socket.js'
	import '@/common/css/common.css'
	export default {
		data() {
			return {
				intervalTimer: null
			}
		},
		globalData: {
			role: '',
			setToken(key, value) {
				return request.setauthorization(key, value);
			},
			getToken(key) {
				return request.getauthorization(key);
			},
			domain: request.domain,
			request(obj) {
				// 逻辑处理
				return request.request(obj, this);
			}
		},
		methods: {
			init() {
				if(!this.globalData.getToken("ucId")) {
					return
				} else {
					clearInterval(this.intervalTimer)
				}
				
				let uid = this.globalData.getToken("ucId")
				if (this.globalData.socketObj) {
					// 如果sockt实例未连接
					if (!this.globalData.socketObj.isConnect) {
					  this.globalData.socketObj.initSocket()
					}
				} else {
					// 如果没有sockt实例，则创建
					this.globalData.socketObj = new WebSocketClass(constants.WEBSOCKETAPI + uid, 3, uid)
					this.globalData.socketObj.initSocket()
				}
			},
		},
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
		},
		onShow: function() {
			console.log('App Show')
			if(!this.globalData.getToken("ucId")) {
				this.intervalTimer = setInterval(() => {
				  this.init()
				}, 3000)
			}else {
				this.init()
			}
		},
		onHide: function() {
			console.log('App Hide')
		}

	}
</script>

<style  lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "colorui/iconfont.css";
</style>
